package cn.tedu.wufl.xcw.main.springboot.config;

import cn.tedu.wufl.xcw.adapter.exception.BusinessException;
import cn.tedu.wufl.xcw.adapter.response.AppCode;
import cn.tedu.wufl.xcw.adapter.response.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice(basePackages = {"cn.tedu.xcw.controller"})
@Slf4j
public class GlobalControllerExceptionHandler {

    @ExceptionHandler({MethodArgumentNotValidException.class})
    public ResultVO bindExceptionHandler(MethodArgumentNotValidException e){
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        return new ResultVO(AppCode.VALIDATE_ERROR,objectError.getDefaultMessage());

    }

    @ExceptionHandler({BusinessException.class})
    public ResultVO apiExceptionHandler(BusinessException e){
        log.error(e.getMsg(),e);
        return new ResultVO(e.getCode(),e.getMsg(),e.getMessage());

    }


}
